import java.awt.Component;
import java.awt.Graphics;
import java.util.Enumeration;

public class TablePile extends CardPile {

	public TablePile(int x1, int y1, int c) {
		super(x1, y1);
		for (int i = 0; i < c; i++) {
			addCard(Solitaire.deckPile.pop());
		}
		top().flip();
	}

	public boolean canTake(Card aCard) {
		if (isEmpty())
			return aCard.getRank() == 12;
		Card topCard = top();
		return (aCard.color() != topCard.color())
				&& (aCard.getRank() == topCard.getRank() - 1);
	}

	public boolean includes(int tx, int ty) {
		return x <= tx && tx <= x + Card.width && y <= ty;
	}

	public void display(Component c,Graphics g){
		int locale = y;
		for(Enumeration e = thePile.elements();e.hasMoreElements();){
			Card aCard = (Card)e.nextElement();
			aCard.draw(c,g, x, locale);
			locale += 17;
				}
	}

	public void select(int tx, int ty) {
		if (isEmpty())
			return;

		Card topCard = top();
		if (!topCard.isFaceup()) {
			topCard.flip();
			return;
		}
		topCard = pop();
		for (int i = 0; i < 4; i++)
			if (Solitaire.suitPile[i].canTake(topCard)) {
				Solitaire.suitPile[i].addCard(topCard);
				return;
			}
		for (int i = 0; i < 7; i++)
			if (Solitaire.tablePile[i].canTake(topCard)) {
				Solitaire.tablePile[i].addCard(topCard);
				return;
			}
		addCard(topCard);

	}

}
